package com.sztx.pay.center.dataaccess.dao.redis.impl;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.RedisConnection;

import com.sztx.se.common.util.StringUtil;
import com.sztx.se.dataaccess.redis.callback.RedisCallback;
import com.sztx.se.dataaccess.redis.impl.BaseRedisDAOImpl;

/**
 * fuquanemail@gmail.com 2016/9/8 11:07
 * description:
 */
public class AbstractRedisDAOImpl extends BaseRedisDAOImpl {
	protected Logger logger = LoggerFactory.getLogger(getClass());


    protected void invalidation(final String redisKey) {
        redisTemplate.execute(new RedisCallback<Boolean>() {
            @Override
            public String getKey() {
                return redisKey;
            }

            @Override
            public Boolean doInRedis(RedisConnection connection, byte[] key) {
            	
            	Long tmp = 0l;
            	
            	if (StringUtil.isNotBlank(redisKey) && redisKey.contains("*")) {					
            		Set<byte[]> keys = connection.keys(key);
            		if (keys != null && !keys.isEmpty()) {
						tmp = connection.del(keys.toArray(new byte[keys.size()][]));
					}
				}else {
					tmp = connection.del(key);
				}
            	
                logger.info("delete {} {} redis cache data ", tmp, getKey());
                return Boolean.TRUE;
            }
        });
    }
}
